/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using EnvDTE;
using Microsoft.Build.BuildEngine;
using DaveSexton.DocProject.Resources;

namespace DaveSexton.DocProject
{
	internal static class ProjectPropertyWriter
	{
		public static void WriteProperties(string projectName, BuildPropertyGroup properties, string outputFile, bool append)
		{
			if (properties == null)
				throw new ArgumentNullException("properties");

			if (string.IsNullOrEmpty(outputFile))
				throw new ArgumentException(Errors.NullOrEmpty, "outputFile");

			using (StreamWriter writer = new StreamWriter(outputFile, append))
			{
				writer.Write("Project Name: " + projectName);
				writer.WriteLine();
				writer.WriteLine();
				writer.WriteLine("    {0,-50}{1,-25}Value", "Property", "Type");
				writer.WriteLine();

				int i = 1;

				foreach (BuildProperty property in properties)
				{
					string number = i++ + ")";
					writer.Write("{0,-4}", number);

					string name = property.Name;
					writer.Write("{0,-50}", name);

					string value = property.FinalValue;

					string type = (value == null) ? "[null]" : value.GetType().Name;
					writer.Write("{0,-25}", type);

					writer.WriteLine(value);
				}
			}
		}

		public static void WriteProperties(string projectName, Properties properties, string outputFile, bool append)
		{
			if (properties == null)
				throw new ArgumentNullException("properties");

			if (string.IsNullOrEmpty(outputFile))
				throw new ArgumentException(Errors.NullOrEmpty, "outputFile");

			using (StreamWriter writer = new StreamWriter(outputFile, append))
			{
				writer.Write("Project Name: " + projectName);
				writer.WriteLine();
				writer.WriteLine();
				writer.WriteLine("    {0,-50}{1,-25}Value", "Property", "Type");
				writer.WriteLine();

				int i = 1;

				foreach (Property property in properties)
				{
					string number = i++ + ")";
					writer.Write("{0,-4}", number);

					string name = property.Name;
					writer.Write("{0,-50}", name);

					object value = VSProjectProperties.GetValue(property);

					object[] values = value as object[];

					if (values != null)
						WritePropertyValues(writer, values);
					else
					{
						writer.Write("{0,-25}", (value == null) ? "[null]" : value.GetType().Name);
						writer.WriteLine(value);
					}
				}
			}
		}

		private static void WritePropertyValues(StreamWriter writer, object[] values)
		{
			foreach (object value in values)
			{
				string valueAsString = value as string;

				if (valueAsString != null && valueAsString.StartsWith("[Error:", StringComparison.OrdinalIgnoreCase))
				{
					writer.Write(new string(' ', 25));

					writer.Write(value);
				}
				else
				{
					string type = (value == null) ? "[null]" : value.GetType().Name;

					writer.Write("{0,-25}", type);

					string displayValue = (value == null) ? string.Empty : value.ToString();

					writer.Write(displayValue);
				}

				writer.WriteLine();
			}
		}
	}
}
